package com.serkol.ams.controller.api;


import com.serkol.ams.bean.Result;
import com.serkol.ams.entity.Printers;
import com.serkol.ams.service.PrintersService;
import com.serkol.annotation.IsLogin;
import com.serkol.constant.ResultConstant;
import net.xpyun.platform.opensdk.service.PrintService;
import net.xpyun.platform.opensdk.service.PrintServiceImpl;
import net.xpyun.platform.opensdk.util.Config;
import net.xpyun.platform.opensdk.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Hesenjan(Serkol)
 * @since 2021-11-17
 */

@RestController
@RequestMapping("api/cdn/sys/prenter")
public class SysPrenterApiController {
    @Autowired
    private PrintersService service;



    @Value("${agentType}")
    private int agentType;


    @IsLogin
    @GetMapping("findByList")
    public Result list() {
        List<Printers> list = service.list();
        return !list.isEmpty() ? Result.ok(list) : Result.failure(ResultConstant.EMPTIY);
    }



    @IsLogin
    @PostMapping("edit")
    public Result edit(@RequestBody Printers data) {



            boolean flag = false;
        if (data.getId() != null && data.getId() > 0) {

                flag = service.updateById(data);
            } else {
            PrintService printService=new PrintServiceImpl();
            AddPrinterRequest request = new AddPrinterRequest();
            // 添加公共参数
            Config.createRequestHeader(request);
            //打印机列表
            List<AddPrinterRequestItem> itemList = new ArrayList<>();

            AddPrinterRequestItem item = new AddPrinterRequestItem();

            //无效打印机编号,此处只是为了举例说明接口返回参数使用，在实际开发中，此处均用有效的打印机编号
            item.setSn(data.getSerialId());
            item.setName(data.getName());
            itemList.add(item);


            //*必填*：items:数组元素为 json 对象：
            //{"name":"打印机名称","sn":"打印机编号"}
            //其中打印机编号 sn 和名称 name 字段为必填项，每次最多添加50台
            AddPrinterRequestItem[] items = new AddPrinterRequestItem[itemList.size()];
            itemList.toArray(items);
            request.setItems(items);

                ObjectRestResponse<PrinterResult> resp = printService.addPrinters(request);
                //resp.data:返回1个 json 对象，包含成功和失败的信息，详看https://www.xpyun.net/open/index.html示例
                 int code = resp.getCode();
            System.out.println(resp);
                 if(code==0) {


                     flag = service.save(data);
                 }
            }

            return flag ? Result.ok() : Result.failure(ResultConstant.FAIL);


    }

    @IsLogin
    @PostMapping("refresh")
    public Result refresh() {



        boolean flag = false;

List<String>  serialIds=service.serialIds();
if(serialIds!=null) {
    PrintService printService = new PrintServiceImpl();

    PrintersRequest request = new PrintersRequest();
    Config.createRequestHeader(request);
 /*       //*必填*：打印机编号
        List<String> snList=new ArrayList<>();
        // 添加打印机设备编号
        snList.add("749ZVK09167AC49");
        //  snList.add("xxxxxxxxxxxxxx");
        //   snList.add("xxxxxxxxxxxxxx");
        //   snList.add("xxxxxxxxxxxxxx");*/
    request.setSnlist(serialIds);
    ObjectRestResponse<List<Integer>> resp = printService.queryPrintersStatus(request);
    if(resp.getCode()==0) {
        //resp.data:返回打印机状态值，共三种：
        //0 表示离线
        //1 表示在线正常
        //2 表示在线缺纸
        //备注：离线的判断是打印机与服务器失去联系超过 30 秒
        List<String> successList=new ArrayList<>();
        List<String> errorList=new ArrayList<>();
         List<Integer> data = resp.getData();
        for (int i = 0; i < serialIds.size(); i++) {
             Integer res = data.get(i);
            String serialId=   serialIds.get(i);
            if(res==1){
                successList.add(serialId);
            }else {
                errorList.add(serialId);
            }
        }
        flag=service.updatePrinter(successList,errorList);
        System.out.println(data);
    }else {
        flag=false;
    }
}


        return flag ? Result.ok() : Result.failure(ResultConstant.FAIL);


    }





}

